package com.atguigu.service;

import com.atguigu.mapper.UserMapper;
import com.atguigu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserserviceImpl implements UserService{


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> rfindAll() {
        return userMapper.rfindAll();
    }

    @Override
    public List<User> rfindAll2() {
        long startTime = System.currentTimeMillis();
        String key = "userList";
        List<User> userList = null;
        if(redisTemplate.hasKey(key)){
           userList = (List<User>) redisTemplate.opsForValue().get(key);
           long endTime = System.currentTimeMillis();
           System.out.println("从缓存中获取数据，耗时：" + (endTime - startTime) + "ms");
        }else{
             userList = userMapper.rfindAll2();
            long endTime = System.currentTimeMillis();
            System.out.println("从数据库中获取数据，耗时：" + (endTime - startTime) + "ms");
            redisTemplate.opsForValue().set(key,userList);
        }
        return userList;
    }
}
